package com.mcptt.http.operator.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.cloudptt.api.product.log.Log;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mcptt.comm.CommonFunc;
import com.mcptt.http.operator.IHttpOpCb;
import com.mcptt.http.operator.OkHttpUtils;

import android.os.Handler;
import android.os.Message;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UploadFileLoader implements Callback{
//	private static final String HTTP_KEY = "http://";;
//	private static final String Sub_Url = ":8080/portal/1001";;
	private static final String HTTP_KEY = "http://";;
	private static final String Sub_Url = ":8030/xpoc/ue-fse-uploadfd";;
	protected final static int OP_SUC = 0;
	protected final static int OP_FAILED = 1;
	private String url = null;
	private File file = null;
	private long len = 0;
	private String num ="";
	protected IHttpOpCb callback = null;
	private OnUploadListener listener = null;

	public UploadFileLoader(String url,File file,long len,IHttpOpCb callback,OnUploadListener listener,String num)
	{
		this.url = url;
		this.file = file;
		this.len = len;
		this.callback = callback;
		this.listener = listener;
		this.num = num;
	}

	public void upLoad() {
		//final String url = HTTP_KEY + svrIp + Sub_Url;
//		final String url = "http://192.168.101.253/TEST/";
//		final String url = "http://192.168.101.172:5000/share.cgi?ssid=0ueFLOB#0ueFLOB";


		new Thread(new Runnable(){
			@Override
			public void run() 
			{
				try {
					doPostFile(url,file,listener,num);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	private void doPostFile(String url,File file,OnUploadListener listener,String num) throws Exception
	{
		Log.debug(getClass(), "doPostFile url = " + url + " file = " + file);
		OkHttpUtils.getInstance().doPostFile(url, file, len,this, listener,num);
	}

	

	private void sendOpResultMsg(boolean isSuc,String jsonStr)
	{
		if(isSuc)
		{
			Message msg = handler.obtainMessage(OP_SUC);
			msg.obj = jsonStr;
			msg.sendToTarget();
		}
		else
		{
			Message msg = handler.obtainMessage(OP_FAILED);
			msg.obj = jsonStr;
			msg.sendToTarget();
		}
	}

	protected Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch(msg.what)
			{
			case OP_SUC:
				if(callback != null)
				{
					String objStr = msg.obj.toString();
					callback.onOpResult(IHttpOpCb.Id_UpLoad,true,0,objStr);
				}
				break;
			case OP_FAILED:
				if(callback != null)
				{
					callback.onOpResult(IHttpOpCb.Id_UpLoad,false,0,"");
				}
				break;
			}
		}
	};
	@Override
    public void onFailure(Call call, IOException e) {
		Log.debug(getClass(), "onFailure e = " + e);
		sendOpResultMsg(false,"");
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
		Log.debug(getClass(), "onResponse response = " + response);
    	if(response == null)
		{
			sendOpResultMsg(false,"");
			return;
		}
		Log.debug(getClass(), "onResponse response.isSuccessful() = " + response.isSuccessful());
		if(response.isSuccessful())
		{
			InputStream  instreams = response.body().byteStream(); 
			String content = CommonFunc.convertStreamToString(instreams);//.convertStreamToString(instreams,"user_not_registered");
			Log.debug(getClass(), "onResponse Content = " + content);
			
			JsonParser jsonParser = new JsonParser();
			JsonObject obj = jsonParser.parse(content).getAsJsonObject();
			Log.debug(getClass(), "onResponse obj = " + obj);
			String url = obj.get("URL").toString();
			if(url != null)
			{
				url = url.replaceAll("\"", "");
			}
			String FileName = obj.get("FileName").toString();
			String allUrl = url + "/" + url;
			obj.addProperty("allUrl", allUrl);
			
			Log.debug(getClass(), "onResponse url = " + url + " FileName = " + FileName + " obj = " + obj.toString());
			
			sendOpResultMsg(true,obj.toString());
		}
		else
		{
			sendOpResultMsg(false,"");
		}
    }
}
